package com.devgroupvn.dota;

import java.io.IOException;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.adwhirl.AdWhirlLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class WebViewNews extends Activity
{

	@Override
	public void onBackPressed()
	{
		super.onBackPressed();
	}
	WebView mWebview;
	TextView tvLoading;
	ProgressBar pb;
	String link;
	AdView adView;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webviewnews);
		mWebview = (WebView)findViewById(R.id.webview);
		tvLoading = (TextView)findViewById(R.id.tvLoading);
		pb=(ProgressBar)findViewById(R.id.progressbar1);
		adView = (AdView)findViewById(R.id.adView);
		
		Bundle bundle= getIntent().getExtras();
		
		mWebview.setWebChromeClient(new WebChrome());
		mWebview.setWebViewClient(new Webclient());
    	
		mWebview.getSettings().setJavaScriptEnabled(true);
		mWebview.getSettings().setBuiltInZoomControls(true);
		mWebview.setHorizontalScrollBarEnabled(false);
		mWebview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		mWebview.setVerticalScrollbarOverlay(true);
		if(bundle!=null)
		{
			link = bundle.getString("link");
//			new async().execute();
			mWebview.loadUrl(link);
//			mWebview .loadData(data, mimeType, encoding)
		}
		setupAdwhirl();
	}
	
	private LinearLayout adLayout;
	private AdWhirlLayout adWhirlLayout;
	private void setupAdwhirl()
	{
		/*adLayout = (LinearLayout) findViewById(R.id.adLayout);
		AdWhirlTargeting.setTestMode(Configs.TEST_MODE_AD);
		AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);
		adWhirlLayout = new AdWhirlLayout(this, Configs.ADWHIRL_KEY_ID_JP);
		final int DIP_WIDTH = 320;
		final int DIP_HEIGHT = 52;
		final float DENSITY = getResources().getDisplayMetrics().density;
		int scaledWidth = (int) (DENSITY * DIP_WIDTH + 0.5f);
		int scaledHeight = (int) (DENSITY * DIP_HEIGHT + 0.5f);
		LinearLayout.LayoutParams adWhirlLayoutParams = new LinearLayout.LayoutParams(scaledWidth, scaledHeight);
		adWhirlLayoutParams.gravity = Gravity.CENTER;
		adLayout.addView(adWhirlLayout, adWhirlLayoutParams);*/
//		AdRequest request = new AdRequest();
//		 adView.loadAd(request);
		try
		{
			final Handler handle = new Handler();
			final Runnable run = new Runnable()
			{

				@Override
				public void run()
				{
					AdRequest request = new AdRequest();
					adView.loadAd(request);
					handle.postDelayed(this, 5000);

				}
			};
			handle.postDelayed(run, 5000);
		}
		catch (Exception e)
		{

		}
	}
	
	private class Webclient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {

			view.loadUrl(url);
			
			return true;
		}
	}
	
	private class async extends AsyncTask<Void, Void, Void>{
		String str;
		boolean isError = false;
		@Override
		protected Void doInBackground(Void... params) {
			NewsArchive mWebContent = new NewsArchive();
			
			try {
				Post post = mWebContent.getPost(link);
				str =URLEncoder.encode(post.content, "utf-8") .replaceAll("\\+", " ");
			} catch (IOException e) {
				isError = true;
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if(!isError)
			{
				mWebview.loadData(str, "text/html", "utf-8");
			}
			else
			{
				finish();
			}
		}
		
	}

	private class WebChrome extends WebChromeClient{

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			super.onProgressChanged(view, newProgress);
			  {
                  if(newProgress < 100){
                      pb.setVisibility(View.VISIBLE);
                      tvLoading.setVisibility(View.VISIBLE);
                      tvLoading .setText(newProgress + "%");
                  }
                  pb.setProgress(newProgress);
                  if(newProgress == 100) {
                      pb.setVisibility(ProgressBar.GONE);
                      tvLoading.setVisibility(View.GONE);
                  }
               }
		}
		
	}


}
